package com.m7788.screen;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.m7788.adapter.ImageProAdapter;
import com.m7788.adapter.ImageProAdapter.OnAdapterChangeListener;
import com.m7788.dao.UploadProDataDaoImpl;
import com.m7788.po.UploadProData;
import com.m7788.util.Constant;
import com.m7788.view.UmengBaseActivity;
import com.tool.custom.progressbar.CProgressBar;

public class UploadInfoScreen extends UmengBaseActivity implements OnClickListener,OnAdapterChangeListener,OnItemClickListener{
	ListView listView;
	Button btn_left;
	ImageView iv_pro;
	AnimationDrawable animPro;
	public Handler handler = new Handler();
	 CProgressBar cpb;
	List<UploadProData> dataList;
	private static UploadProDataDaoImpl daoImpl;
	ImageProAdapter ipa;
	CommandReceiver cmdReceiver;
	private int po;
	 private class CommandReceiver extends BroadcastReceiver{
	       @Override
	       public void onReceive(Context context, Intent intent) {
	        if(intent.getAction().equals(Constant.ACTION_STOA)){
	        	   Bundle bundle = intent.getExtras();
		    	   int operation = bundle.getInt("operation",-1);
		    	   if(operation==1){//
		    		   refreshData();		    		   
		    		   //finish();
		    	   }else  if(operation==2){
		    		   refreshData();		    		  
		    		   finish();

		    	   }else  if(operation==3){
		    		   
		    		   finish();
		    	   }
	                 
	          }
	       }                        
	   }
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.upload_info_screen);
        daoImpl = new UploadProDataDaoImpl(this);
        initView();
    }
    @Override
    public void onResume() {
        super.onResume();
        handler.postDelayed(new Runnable() {  
            public void run() {  
           	 animPro.start();
            }
       }, 50);
        cmdReceiver = new CommandReceiver();
        IntentFilter filter = new IntentFilter();//创建IntentFilter对象
        //注册一个广播，用于接收Activity传送过来的命令，控制Service的行为，如：发送数据，停止服务等
        filter.addAction(Constant.ACTION_STOA);
        //注册Broadcast Receiver
        registerReceiver(cmdReceiver, filter);
        
    }
    
    @Override
    public void onDestroy() {
    	animPro.stop();
        this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver
        super.onDestroy();
    }
    private void initView(){
    	btn_left =(Button)findViewById(R.id.uis_btn_left);
    	btn_left.setOnClickListener(this);
    	
    	iv_pro = (ImageView)findViewById(R.id.uis_iv_pro);
    	animPro = (AnimationDrawable) iv_pro.getBackground();
    	
    	cpb =  (CProgressBar)findViewById(R.id.uis_cpb);
//    	listView =  (ListView)this.findViewById(R.id.uis_listview) ;
//		listView.setOnItemClickListener(this);
//		listView.setCacheColorHint(0);
//		listView.setDivider(getResources().getDrawable(R.drawable.listview_line));
		refreshData();
    }
    
    
    private  synchronized   void refreshData(){
//    	dataList = daoImpl.find();
    	
    	dataList = daoImpl.rawQuery(
				"select * from UploadProData order by id desc ", new String[] {
						 });
    	/*
		ipa = new ImageProAdapter(this ,dataList);
    	ipa.setOnAdapterChangeListener(this);
    	listView.setAdapter(ipa);
    	*/
    	if(dataList!=null && dataList.size()>0){
    		
    		 UploadProData info = dataList.get(0);
    		 cpb.setMax(info.getTotal());
    		 cpb.setProgress(info.getCur(), info.getCur()+"/"+info.getTotal());
    		 
//    		List<UploadProData> tempList = new ArrayList<UploadProData>();
//    		tempList.add(dataList.get(0));
//    		ipa = new ImageProAdapter(this ,tempList);
//        	ipa.setOnAdapterChangeListener(this);
//        	listView.setAdapter(ipa);
    	}
    }
    
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.uis_btn_left:
			finish();
    		break;
    	default:
    		break;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
	}
	@Override
	public void listRemove(int currentItem) {
//		po = currentItem;
//		
//		Dialog dialog = new AlertDialog.Builder(UploadInfoScreen.this)
//        .setTitle("确定删除？")   
//        .setPositiveButton("确定",
//                new DialogInterface.OnClickListener() {   
//                    @Override   
//                    public void onClick(DialogInterface dialog,   
//                            int which) {   
//                    	dialog.dismiss();
//                    	ipa.removeData(po);
//                    }   
//                }).setNegativeButton("取消",    
//                new DialogInterface.OnClickListener() {   
//                    public void onClick(DialogInterface dialog,   
//                            int whichButton) {   
//                    	dialog.dismiss();
//                    }   
//                }).create();
//
//			dialog.show(); 
	}
	
	
	
}